package ssn.currentsystem.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import ssn.common.util.ExcelUploadUtil;
import ssn.currentsystem.service.CurrentSystemService;
import ssn.currentsystem.vo.CurrentSystemVO;
import egovframework.rte.fdl.cmmn.AbstractServiceImpl;
import egovframework.rte.fdl.property.EgovPropertyService;

/**  
 * @Class Name : 
 * @Description : 
 * 
 * @author ksh
 * @since 2013. 09.26
 * @version 1.0
 * @see
 */
@SuppressWarnings({"rawtypes","unchecked"})
@Service("currentSystemService")
public class CurrentSystemServiceImpl extends AbstractServiceImpl implements CurrentSystemService {
	
	@Resource(name="currentSystemDAO")
	private CurrentSystemDAO currentSystemDAO; 
	
	@Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
	
	/**
	 * 설치현황 정보 입력
	 */
	public void insertCurrentSystemInfo(CurrentSystemVO vo) throws Exception{
		vo.setScs_sys_key(currentSystemDAO.selectCurrentSystemInfoPk(vo.getScs_area_key()));
		currentSystemDAO.insertCurrentSystemInfo(vo);
	}
	
	/**
	 * 설치현황 Excel 정보 입력
	 */
	public Map insertCurrentSystemInfoExcel(Map map) throws Exception{
		String file = propertiesService.getString("file.temp.path") + File.separator + (String)map.get("scs_excel_file");
		ExcelUploadUtil excelUtil = new ExcelUploadUtil(file, 1);
		
		List excelList = excelUtil.loadExcel((String[])map.get("cellInfo"));
		int totalRow = excelUtil.loadExcelTotalCnt();
		
		int nextPk = Integer.parseInt(currentSystemDAO.selectCurrentSystemInfoPk((String)map.get("scs_area_key")));
		for(int i=0;i<excelList.size();i++){
			((Map)excelList.get(i)).put("scs_sys_key", (nextPk+i));
		}
		map.put("excelList", excelList);
		
		Map resultMap = null;
		try{
			currentSystemDAO.insertCurrentSystemInfoExcel(map);

			resultMap = new HashMap();
			resultMap.put("totalRow", totalRow);
			resultMap.put("insertRow", excelList.size());
		}catch(Exception e){
			return null;
		}
		return resultMap;
	}
	
	/**
	 * 설치현황 정보 수정
	 */
	public void updateCurrentSystemInfo(CurrentSystemVO vo) throws Exception{
		currentSystemDAO.updateCurrentSystemInfo(vo);
	}
	/**
	 * 설치현황 정보 수정
	 */
	public void updateCurrentSystemOtherInfo(CurrentSystemVO vo) throws Exception{
		currentSystemDAO.updateCurrentSystemOtherInfo(vo);
	}
	
	/**
	 * 설치현황 정보 삭제(DB 삭제) 
	 */
	public void deleteCurrentSystemInfo(CurrentSystemVO vo) throws Exception{
		currentSystemDAO.deleteCurrentSystemInfo(vo);
	}
	
	/**
	 * 설치현황 정보 리스트 및 전체 카운트
	 */
	public Map selectCurrentSystemList(CurrentSystemVO vo) throws Exception{
		Map resultMap = new HashMap();
		resultMap.put("resultList", currentSystemDAO.selectCurrentSystemList(vo));
		resultMap.put("resultListCnt", currentSystemDAO.selectCurrentSystemListCnt(vo));
		return resultMap;
	}
	/**
	 * 설치현황 정보 조회
	 */
	public CurrentSystemVO selectCurrentSystemInfo(CurrentSystemVO vo) throws Exception{
		return currentSystemDAO.selectCurrentSystemInfo(vo);
	}
	
	/**
	 * 설치현황 정보 리스트 검색
	 */
	public List selectSearchCurrentSystem(Map paramMap) throws Exception{
		return currentSystemDAO.selectSearchCurrentSystem(paramMap);
	}
}
